from datetime import datetime
from typing import List, Any, Optional, Dict
from pydantic import BaseModel


class ScanRequest(BaseModel):
    """扫描请求"""
    repo_url: str
    branch: str
    scan_type: str = "all"  # 默认为全量扫描


class TaskCreate(BaseModel):
    """创建任务请求"""
    repo_url: str
    branch: str
    scan_type: str = "all"


class TaskStatus(BaseModel):
    """任务状态"""
    id: int
    status: str
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


class ScanResultSchema(BaseModel):
    """扫描结果"""
    id: int
    task_id: int
    file_path: str
    function_name: str
    start_line: int
    end_line: int
    issue_type: str
    severity: str
    message: str
    suggestion: Optional[str] = None
    created_at: datetime

    class Config:
        from_attributes = True


class TaskWithResults(BaseModel):
    """任务及其结果"""
    id: int
    repo_url: str
    branch: str
    scan_type: str
    status: str
    created_at: datetime
    updated_at: datetime
    results: List[ScanResultSchema] = []

    class Config:
        from_attributes = True


class ScanRecordSchema(BaseModel):
    """扫描记录"""
    id: int
    scan_id: int
    scan_type: str
    status: str
    start_time: datetime
    end_time: Optional[datetime] = None
    total_files: int
    scanned_files: int
    total_issues: int
    scan_config: Optional[Dict[str, Any]] = None
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


class ScanTaskListSchema(BaseModel):
    """扫描任务列表项"""
    id: int
    repo_url: str
    branch: str
    scan_type: str
    status: str
    created_at: datetime
    updated_at: datetime
    total_issues: int = 0  # 该任务发现的问题总数

    class Config:
        from_attributes = True


class ScanTaskListResponse(BaseModel):
    """扫描任务列表响应"""
    total: int
    page: int
    page_size: int
    items: List[ScanTaskListSchema]

    class Config:
        from_attributes = True


class ScanResultDetailSchema(BaseModel):
    """扫描结果详情"""
    id: int
    file_path: str
    function_name: str
    line_number: str
    start_line: int
    end_line: int
    issue_type: str
    severity: str
    message: str
    code_snippet: str
    created_at: datetime

    class Config:
        from_attributes = True


class ScanResultDetailListResponse(BaseModel):
    """扫描结果详情列表响应"""
    total: int
    page: int
    page_size: int
    items: List[ScanResultDetailSchema]

    class Config:
        from_attributes = True


class ScanResultDetailRequest(BaseModel):
    """扫描结果详情列表请求"""
    page: int = 1
    page_size: int = 10
    file_path: Optional[str] = None
    function_name: Optional[str] = None
    issue_type: Optional[str] = None
    severity: Optional[str] = None

    class Config:
        from_attributes = True


class ChatMessage(BaseModel):
    """聊天消息"""
    role: str  # "user" 或 "assistant"
    content: str


class KnowledgeBaseChatRequest(BaseModel):
    """知识库聊天请求"""
    query: str
    knowledge_base_name: str = "samples"
    history: Optional[List[ChatMessage]] = None
    stream: bool = True
    conversation_id: Optional[str] = None
    model_name: Optional[str] = None
    web_search_enabled: bool = False
    max_history_length: int =5  # 最大历史记录长度，防止token超限
    include_history: bool = True  # 是否包含历史记录
